library(tidyverse)

counts=read_csv(  "../county_change_counts.csv")

shock = read_csv("../shock_county.csv")

shocks=shock %>% 
  mutate(key = paste(state,county,sep="-"))

check= shocks%>%
  filter(key %in% counts$key)

count_data = inner_join(shocks,counts,by="key", suffix = c("", ".y"))
count_data = count_data %>%
  mutate(change_type = case_when(counts<0~'removed',
                                 counts>0~'added',
                                 counts==0~'moved'))
count_data %>% 
  filter(state %in% c("ia","in","wi","pa","nc","md") )%>% 
  group_by(change_type) %>% 
  summarize(n=n()) %>% 
  mutate(freq = n / sum(n))

count_data %>% 
  filter(state %in% c("ia","in","wi","pa","nc","md") )%>% 
  mutate(uid = paste0(county,'-',state))%>% 
  group_by(change_type) %>% 
  summarize(n=length(unique(uid))) %>% 
  mutate(freq = n / sum(n))


